#!/usr/bin/env python

from google.appengine.ext import db 
from google.appengine.api import memcache

# These classes define the data objects
# that you will be able to store in
# AppEngine's data store.

class Setting(db.Model):
# Required properties
    name = db.StringProperty(required=True)
    value = db.StringProperty(multiline=True)

    @staticmethod
    def get(name, default):
    
        key = "Setting:" + name
        value = memcache.get(key)
        if value is not None:
            return value
            
        setting = Setting.resolve(name)
        if setting:
            memcache.set(key, setting.value)
            return setting.value
        
        return default

    @staticmethod
    def save(name, value):
        setting = Setting(
            key_name = name,
            name = name,
            value = value)
        setting.put()
        memcache.set("Setting:" + name, setting.value)

    @staticmethod
    def resolve(name):
        q = Setting.all()
 
        q.filter("name", name)
        
        settings = q.fetch(1)
        return settings[0] if len(settings) > 0 else None

